﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace FieldDevice.UI
{
    public partial class Thermometer : UserControl
    {
        public Thermometer()
        {
            InitializeComponent();
        }

        private float Temperature_Value = 0.0f;
        private SolidBrush temper_Color = new SolidBrush(Color.Yellow);
        private SolidBrush alarm_Color = new SolidBrush(Color.Red);
        private float alarmValue = 60.0f;           //温度计的超温限值

        #region 属性
        [Category("Thermometer"), Description("温度计高度")]
        public float VerticalDegree 
        { 
            get {
            if (this.Height <= this.Width)
                return this.Width;
            else
            return this.Height; } }

        [Category("Thermometer"), Description("温度计宽度")]
        public float HoriDegree 
        { 
            get 
            {
                if (this.Height <= this.Width)
                    return this.Height;
                else
                return this.Width; 
            } 
        }
        [Category("Thermometer"), Description("温度值[0~80℃]")]
        public float Temper_Value
       {
           get 
         {
               return this.Temperature_Value;  
         }
          set 
          {
              this.Temperature_Value = value;
              this.Refresh();
                
         }
      }

        [Category("Thermometer"), Description("温度计超温限值")]
        public float AlarmValue
        { 
            get
                {
                    return this.alarmValue;
                }
            set
                {
                    this.alarmValue = value;
                    this.Refresh();
                }
        }

        [Category("Thermometer"), Description("正常温度时温度计的颜色")]
        public Color TemperatureColor
        {
            get
            {
                return this.temper_Color.Color;
            }
            set
            {
                this.temper_Color.Color = value;
                this.Refresh();
            }
        }
        [Category("Thermometer"), Description("超温时温度计超温部分的颜色")]
        public Color AlarmColor
        {
            get
            {
                return this.alarm_Color.Color;
            }
            set
            {
                this.alarm_Color.Color = value;
                this.Refresh();
            }
        }

        #endregion

       private  float Thermometer_Hi()
        { 
            return (0.85f*VerticalDegree-0.4f*HoriDegree)*0.7f;
        }

        private float Temperature_Zero()                            //0℃时的坐标位置
        {
            return 0.78f * VerticalDegree - 0.32f * HoriDegree;
        }


        private void Thermometer_Paint(object sender, PaintEventArgs e)
        {
            float value_len = (this.Temperature_Value * Thermometer_Hi()) / 80.0f;
            Graphics graphic = e.Graphics;
            Pen framePen = new Pen(Color.White,2.0f);
            RectangleF rect = new RectangleF(0.3f * HoriDegree, 0.95f * VerticalDegree - 0.4f * HoriDegree, 
                                                0.4f * HoriDegree, 0.4f * HoriDegree);
          
            graphic.DrawArc(framePen, rect, -60.0f, 300.0f);

            graphic.DrawLine(framePen, new PointF(0.4f * HoriDegree, 0.95f * VerticalDegree - 0.3732f * HoriDegree),
                                       new PointF(0.4f * HoriDegree, 0.05f * VerticalDegree));

            graphic.DrawLine(framePen, new PointF(0.6f * HoriDegree, 0.95f * VerticalDegree - 0.3732f * HoriDegree),
                                       new PointF(0.6f * HoriDegree, 0.05f * VerticalDegree));

            graphic.DrawLine(framePen, new PointF(0.4f * HoriDegree, 0.05f * VerticalDegree),
                                       new PointF(0.6f * HoriDegree, 0.05f * VerticalDegree));

            graphic.FillEllipse(temper_Color, 0.35f * HoriDegree, 
                                                0.95f * VerticalDegree - 0.35f * HoriDegree,
                                                                0.3f * HoriDegree, 0.3f * HoriDegree);
            graphic.FillRectangle(temper_Color, 0.425f * HoriDegree,
                                            Temperature_Zero(),
                                            0.15f * HoriDegree,0.17f*VerticalDegree+0.12f*HoriDegree);
            for (int i=0; i < 5; i++)
            {
                graphic.DrawString((20 * i).ToString() , new Font(Font, FontStyle.Bold), temper_Color, 0.6f * HoriDegree,
                                    Temperature_Zero() - Thermometer_Hi() * i / 4.0f - this.Font.Size / 2.0f);
            }

            if (Temper_Value <= alarmValue)
            {
                graphic.FillRectangle(temper_Color, 0.425f * HoriDegree, (Temperature_Zero() - value_len)
                   , 0.15f * HoriDegree, value_len);
            }
            else
            {
                graphic.FillRectangle(temper_Color, 0.425f * HoriDegree, (Temperature_Zero() - (alarmValue * Thermometer_Hi())/80.0f),
                    0.15f * HoriDegree, (alarmValue * Thermometer_Hi()) / 80.0f);
                graphic.FillRectangle(alarm_Color, 0.425f * HoriDegree, (Temperature_Zero() - value_len),
                    0.15f * HoriDegree, ((value_len - (alarmValue * Thermometer_Hi()) / 80.0f)));
            
            }  
            graphic.Dispose();
            framePen.Dispose();
        }
    }
}
